import { Injectable } from "@nestjs/common";
import { Request } from "express";
import { Strategy } from "passport-jwt"
import { JwtService } from "@nestjs/jwt"
import { PassportStrategy } from "@nestjs/passport"


@Injectable()
export class JwtStrategy extends PassportStrategy(Strategy) {
    private getHeaderToken(req: Request) {
        const [type, token] = req.headers.authorization?.split(' ') ?? [];
        return type === 'Bearer' ? token : undefined;
    }
    private getCookieToken(req: Request) {
        if (!req.headers.cookie) return null;
        const { token } = req.headers.cookie.split(";").reduce((acc, label) => {
            const split = label.split("=");
            acc[split[0].trim()] = split[1];
            return acc;
        }, {} as { token?: string });
        return token
    }
    constructor(
        private readonly jwtService: JwtService
    ) {
        super({
            jwtFromRequest: (req: Request) => {
                return this.getHeaderToken(req) || this.getCookieToken(req)
            },
            ignoreExpiration: true,
            secretOrKey: "sbppk",
        })
    }

    async validate({

    }) {

    }
}
